var db = require('./index.js');
var uuid = require("node-uuid");
var co = require('co');
var crypto = require('crypto');

var User = db.models.User;
var Story = db.models.Story;
var Comment = db.models.Comment;
var Chat = db.models.Chat;
var Msg = db.models.Msg;

function getToken(str,encoding){
    encoding = encoding || "base64";
    return crypto.createHash("sha1").update(str.toString()).digest(encoding);
}

function * production() {
    //db.sync({force:true});
}

function * UserSeed() {
    console.log("UserSeed");
    for (var x = 0; x < 10; x++) {
        var token = getToken("1834086171" + x + "123456");
        yield User.create({
            mobile: "1834086171" + x,
            password: "123456",
            userName: "test",
            avatar: "http://www.baidu.com",
            sign: "hello world",
            token: token,
            followingEnable: x % 2,
            followerEnable : x % 2,
            aboutNotice : x % 2,
            updateNotice : x % 2,
            noticeEnable : x % 2
        });
    }
}

function * FollowSeed(){
    console.log("FollowSeed");
    var users = yield User.findAll();
    for(var x = 0;x < users.length;x ++){
        for(var y = 0;y < users.length ;y++){
            if(x === y) break;
            users[x].addFollower(users[y]);
        }
    }
}

function * StorySeed() {
    console.log("StorySeed");
    var users = yield User.findAll({});
    var stories = [];
    for (var j = 0; j < users.length; j++) {
        for (var i = 0; i < 10; i++) {
            var story = yield Story.create({
                id: uuid.v4(),
                title: " 这是一篇文章",
                flags: "",
                content: "hello world",
                AuthorID: users[j].id
            });
            stories.push(story);
        }
    }
    for(var x = 0;x < users.length;x ++){
        for(var y = 0;y < stories.length ; y++){
            yield users[x].addStories(stories[y],{isLike:1,isCollection:1});
        }
    }
}


function * CommentSeed(){
    console.log("CommentSeed");
    var users = yield User.findAll();
    for(var x = 0;x < users.length - 8;x ++){
        var stories = yield Story.findAll({
            where:{
                AuthorID:users[x].id
            }
        });
        var comments = [];
        for(var i = 0;i < users.length - 8;i ++){
            for(var j = 0;j < stories.length; j++){
                for(var k = 0;k < 1;k ++){
                      var comment =   yield Comment.create({
                            content:"this is a test comment of article",
                            UserId : users[x].id,
                            StoryId: stories[j].id
                        });
                    comments.push({comment:comment,storyId:stories[j].id});
                }
            }
        }
        for(var y = 0;y < comments.length;y ++){
            yield Comment.create({
                content:"this is a test comment of comment",
                UserId: users[x].id,
                StoryId: comments[y].storyId,
                RevCommentId : comments[y].comment.id
            });
        }
    }
}

function * MsgSeed(){
    console.log("MsgSeed");
    var users = yield  User.findAll({});
    for(var x in users){
        for(var i = 0;i < 10;i ++){
            var msg = yield Msg.create({
                sendID:users[x].id,
                revID:users[(x + 1) % users.length].id,
                content: "this is a msg from" + users[x].id + "to" + users[(x + 1) % users.length].id
            });
            var revMsg = yield Msg.create({
                sendID: users[(x + 1) % users.length].id,
                revID: users[x].id,
                content: "this is a msg from" +  users[(x + 1) % users.length].id + "to" +users[x].id
            });
        }
    }
}

function * Seed(){
    var data = yield User.findOne({
        where:{
            id:10
        },
        include:[{
            model:Story,
            as:"MyStory"
        }]
    });
}

function * init() {
    yield db.sync({force: true});
    yield UserSeed();
    yield FollowSeed();
    yield StorySeed();
    yield CommentSeed();
    yield MsgSeed();
    //yield Seed();
}

var pro = process.argv[2] == '--pro';
co(function *() {
    // if(pro){
    //     yield production();
    // }else{
    //     yield init();
    // }
    yield init();
    console.log('finished ...');
    process.exit(0);
}).catch(function (err) {
    console.log(err.stack);
});